package com.hod.roommap.client.model;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.gwtext.client.core.EventCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.DateField;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.VType;
import com.gwtext.client.widgets.form.event.ComboBoxListenerAdapter;
import com.gwtext.client.widgets.grid.AbstractSelectionModel;
import com.gwtext.client.widgets.grid.RowSelectionModel;
import com.gwtext.client.widgets.grid.event.RowSelectionListenerAdapter;
import com.gwtext.client.widgets.layout.ColumnLayout;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.gwtext.client.widgets.layout.FormLayout;
import com.gwtext.client.widgets.layout.RowLayoutData;

public class ClientCRUD extends CRUD {

	protected String idClient = "1";
	
	
	public ClientCRUD() {
		ds = (GearsDataStore) GWT.create(Client.class);
	}
	
	
	
	public ComboBox createClientCombo(){
		ComboBox cb = new ComboBox();		
		cb.setStore(gridPanel.getStore());
		cb.setDisplayField("surname");
		cb.setLabel("Cliente");
		cb.setTypeAhead(false);
		cb.setLoadingText("Searching...");
		cb.setWidth(440);
		cb.setPageSize(10);
		cb.setHideTrigger(true);
		return cb;
	}
	
	public void createClientPanelSearch(Panel panel,ComboBoxListenerAdapter cba, final EventCallback kl){
		Label lblPanelSearchClients =  new Label("Cliente");
		final ComboBox cb = new ComboBox();		
		cb.setStore(gridPanel.getStore());
		cb.setDisplayField("surname");
		cb.setLabel("Cliente");
		cb.setTypeAhead(false);
		cb.setLoadingText("Searching...");
		cb.setWidth(440);
		cb.setPageSize(10);
		cb.setHideTrigger(true);
		cb.addListener(cba);
		cb.addKeyPressListener(new EventCallback(){

			@Override
			public void execute(EventObject e) {
				// TODO Auto-generated method stub
				if (cb.getText().length()==0){
					kl.execute(e);
				}
			}
			
		});
        HorizontalPanel panelTipoHabitacion = new HorizontalPanel();
        panelTipoHabitacion.add(lblPanelSearchClients);
        panelTipoHabitacion.add(cb);
        panel.add(panelTipoHabitacion,new RowLayoutData(15));
	}
	
	protected void buildPanelSearch() {
				
	}

	protected TabPanel formTabs(){
	        
	        return null;
	}

	@Override
	protected AbstractSelectionModel createSelectionModel() {
		// TODO Auto-generated method stub
		 final RowSelectionModel sm = new RowSelectionModel(true);
         sm.addListener(new RowSelectionListenerAdapter() {
         	public void onRowSelect(RowSelectionModel sm, int rowIndex, Record record) {
                 idClient = record.getAsString("idClient");
                 formPanel.updateDataControls(record);
             }
         });
         return sm;
	}

	public void refreshContactData(String idClient) {
		
	}



	@Override
	public HODForm createFormPanel() {
		// TODO Auto-generated method stub
		if (formPanel==null) formPanel = new ClientFormPanel(ds);
		return formPanel;
	}
	

}
